﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;

namespace DARE
{
    [AttrTypeDependentSerializable(typeof(CHorizontalSelectorData))]
    public class CHorizontalSelector : CSelector
    {
        #region actualize

        /// <summary>
        /// Actualizes the widget.
        /// If anything has changed, it will allow this widget to get back its parameters.
        /// For example, if the size of the widget is a percentage of the size of its parent,
        /// if the parent's size changes, it will recalculate the good size of this widget.
        /// (This method is called automatically but you can force a new actualization).
        /// </summary>
        public override void Actualize()
        {
            base.Actualize();
            if (m_selected != null && AdjustSize)
            {
                m_rect.Width = m_selected.Rect.Width + m_nextButton.Rect.Width + m_prevButton.Rect.Width;
                m_rect.Height = m_selected.Rect.Height;
                m_selected.Position = new Vector2(m_prevButton.Rect.Width, 0);
            }
            else if (m_selected != null)
            {
                m_selected.Position = new Vector2(m_prevButton.Rect.Width, 0);
            }
            m_bodyRect.X = (int)(m_rect.X + m_prevButton.Rect.Width);
            m_bodyRect.Width = m_rect.Width - m_prevButton.Rect.Width - m_nextButton.Rect.Width;
            m_bodyRect.Height = m_rect.Height;
            m_texture.Rectangle = m_bodyRect;
            m_nextButton.Position = new Vector2(m_rect.Width - m_nextButton.Rect.Width - Margins.Right, (m_rect.Height / 2) - (m_nextButton.Rect.Height / 2));
            //m_nextButton.Actualize();
            
            m_prevButton.Position = new Vector2(-Margins.Left, (m_rect.Height / 2) - (m_prevButton.Rect.Height / 2));
            //m_prevButton.Actualize();
            m_numPagePos = new Vector2(m_rect.X + (m_rect.Width / 2.0f) - (CDare.Instance.DebugMgr.DebugConsole.SpriteFont.MeasureString((m_selectedIndex + 1) + " / " + m_list.Count).X / 2.0f), m_rect.Y + m_rect.Height);
        }

        #endregion

        #region actionsOnm_list

        /// <summary>
        /// Adds a widget in the horizontal selector
        /// </summary>
        public CHorizontalSelector Add(CWidget widget)
        {
            widget.Size = new Vector2(1, 1);
            widget.Position = new Vector2(0, 0);
            widget.Rect = m_bodyRect;
            m_list.Add(widget);
            widget.Parent = this;
            Actualize();
            return this;
        }

        public void DisplayWidget(CWidget widget)
        {
            if (m_list.Contains(widget))
            {
                m_selectedWidget = widget;
                m_selectedIndex = m_list.IndexOf(m_selectedWidget);
            }
        }

        public CHorizontalSelector Remove(CWidget widget)
        {
            m_list.Remove(widget);
            return this;
        }

        #endregion

        #region ctor

        public CHorizontalSelector(CHUDSystem hud, CWidget parent, string name)
            : base(hud, parent, name)
        {
            m_nextButton.Position = new Vector2(0, (m_rect.Height / 2) - (m_nextButton.Rect.Height / 2));
            m_nextButton.Texture = HUD.Skinner.GetTexture("horizontalselectormorebuttonreleased");
            m_nextButton.ReleasedTexture = HUD.Skinner.GetTexture("horizontalselectormorebuttonreleased");
            m_nextButton.ClickedTexture = HUD.Skinner.GetTexture("horizontalselectormorebuttonclicked");

            m_prevButton.Position = new Vector2(m_rect.Width - (m_prevButton.Rect.Width + Margins.Left + Margins.Right), (m_rect.Height / 2) - (m_prevButton.Rect.Height / 2));
            m_prevButton.Texture = HUD.Skinner.GetTexture("selectorhorizontallessbuttonreleased");
            m_prevButton.ReleasedTexture = HUD.Skinner.GetTexture("horizontalselectorlessbuttonreleased");
            m_prevButton.ClickedTexture = HUD.Skinner.GetTexture("horizontalselectorlessbuttonclicked");

            m_texture = HUD.Skinner.GetTexture("horizontalselector");
            m_bodyRect.X = (int)(m_rect.X + m_prevButton.Rect.Width + Margins.Left);
            m_bodyRect.Width = m_rect.Width - m_prevButton.Rect.Width - m_nextButton.Rect.Width;
            m_texture.Rectangle = m_bodyRect;
            m_numPageColor = Color.White;

            Size = new Vector2(400, 400);
        }

        #endregion

        #region update/draw

        public override bool Update(GameTime gameTime)
        {
            if (m_list.Count > 0)
                m_selected = m_list[m_selectedIndex % m_list.Count];
            return base.Update(gameTime);
        }

        public override bool Draw(GameTime gameTime)
        {
            if (!m_isVisible)
                return false;



            if (m_texture != null)
                m_texture.Draw(gameTime);//m_spriteBatch.Draw(m_texture, m_rect, m_color);
            if (m_selected != null)
            {
                Actualize();
                m_selected.Draw(gameTime);
            }
            if (m_prevButton != null)
            {
                if (m_selectedIndex == 0 && !m_loopEnabled)
                    m_prevButton.Hide();
                else
                    m_prevButton.Show();
            }
            if (m_nextButton != null)
            {
                if (m_selectedIndex == m_list.Count - 1 && !m_loopEnabled)
                    m_nextButton.Hide();
                else
                    m_nextButton.Show();
            }
            m_nextButton.Draw(gameTime);
            m_prevButton.Draw(gameTime);
            if (m_displayNumPage)
                SpriteBatch.DrawString(CDare.Instance.DebugMgr.DebugConsole.SpriteFont, (m_selectedIndex + 1) + " / " + m_list.Count, m_numPagePos, m_numPageColor);


            return true;
        }

        #endregion

    }
}
